package com.sxyc.common;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;

import com.jfinal.render.Render;

/**
 * 返回图片的Render
 * @author WangQian
 *
 */
public class ImageRender extends Render {

	// 使用jFinal 日志
	private com.jfinal.log.Log log = com.jfinal.log.Log.getLog(this.getClass());

	private BufferedImage image = null;
	
	public ImageRender(BufferedImage image){
		this.image = image;
	}
	
	@Override
	public void render() {
		response.setContentType("image/jpeg");
		ServletOutputStream sos = null;
		try {
			sos = response.getOutputStream();
			ImageIO.write(image, "jpeg", sos);
		} catch (Exception e) {
			log.error("renderImage Exception："+e.getMessage());
			throw new RuntimeException(e);
		} finally {
			if (sos != null) {
				try {
					sos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
